import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;
import org.jsoup.*;


public class Parser {
	
	static String url = "http://livescore.com/soccer/live/";
	static String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
	String currentCountry = null;
	String currentLeague = null;
	
	
	public List<Match> getMatches()
	{
		Document doc = null;
		List<Match> listOfMatches = new LinkedList<Match>();
		int homeScore, awayScore; homeScore=awayScore=0;
		String home,away,time; home=away=time=""; 
				
		try {
			 doc = Jsoup.connect(url).userAgent(userAgent).get(); //User agent, maybe?
			 doc.outputSettings().charset("UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if ( doc != null)
		{
			Elements root = doc.select("td[width=468]"); // This is the table that contains the matches
			for (Element row : root.select("tr")) {
				if(row.hasText())
				{
					if(row.attr("bgcolor").equals("#333333")) //New league found
					{
						Elements league = row.select("td");
						setCountryAndLeague(league.get(0).html());
					}
					else
					{
						if(row.attr("bgcolor").equalsIgnoreCase("#CFCFCF") || row.attr("bgcolor").equalsIgnoreCase("#dfdfdf")  ) //I'm 
						{			
							for(Element column : row.select("td"))
							{
								if(column.hasText())
								{
									
									if(column.attr("width").equals("45") && column.attr("height").equals("18")) //Got time
									{
										time = column.text().replaceAll("\\s","").substring(1);
									}
									else
									{
										if(column.attr("align").equals("right") && column.attr("width").equals("186")) //Got home team
										{
											home = column.text();
										}
										else
										{
											if(column.attr("align").equals("center") && column.attr("width").equals("51") ) //Got the result. Example: 0-0
											{
												String results[] = column.text().split(" ");
												homeScore = Integer.parseInt(results[0]);
												awayScore = Integer.parseInt(results[2]);
											}
											else
											{
												if(column.attr("width").equals("186"))//Got away team
												{
													away = column.text();
													
													Match m = new Match();
													m.country = currentCountry;
													m.league = currentLeague;
													m.time = time;
													m.homeTeam = home;
													m.awayTeam = away;
													m.awayScore = awayScore;
													m.homeScore = homeScore;
													listOfMatches.add(m);

												}
												else
													System.err.println("/!\\ SOMETHING WRONG AT MATCH PARSING /!\\");												
											}
										}
									}		
								}
							}
						}
					}
				}
			}
			
			return listOfMatches;
		}
	    return null;
	}
	
	private void setCountryAndLeague(String leagueHTML) //INPUT LIKE THIS: &nbsp;<b>Korea Republic</b> - K-League Classic
	{
		Document doc = Jsoup.parse(leagueHTML);
		Elements a = doc.select("b").remove();
		currentCountry = a.text();
		doc = Jsoup.parse(leagueHTML);
		currentLeague = doc.text().replace("(Classifica)", "").replace(currentCountry,"").substring(4).replaceAll("\\s+$", "");;
	}

	
	



}